package cn.itcast.bos.web.action.base;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends BaseAction<Courier>{
	
	@Autowired
	private CourierService courierService;
	@Action(value="courier_save",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String save(){
		courierService.save(model);
		return SUCCESS;
	}
	
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable=new PageRequest(page-1, rows);
		System.out.println(pageable);
		Specification<Courier> specification=new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				
				Predicate predicate=null;
				if(StringUtils.isNoneBlank(model.getCourierNum())){
					
					predicate=cb.and(cb.equal(root.get("courierNum").as(String.class), model.getCourierNum()));
				}
				if(StringUtils.isNoneBlank(model.getCompany())){
					
					predicate=cb.and(cb.equal(root.get("company").as(String.class), model.getCompany()));
				}
				if(StringUtils.isNoneBlank(model.getType())){
					
					predicate=cb.and(cb.equal(root.get("type").as(String.class), model.getType()));
				}
				
				Join<Courier, Standard> standardJoin=root.join("standard",JoinType.INNER);
				if(model.getStandard()!=null
						&&  StringUtils.isNoneBlank(model.getStandard().getName())){
					predicate=cb.and(cb.equal(standardJoin.get("name").as(String.class),model.getStandard().getName() ));
				}
				return predicate;
			}
		};

		Page<Courier> pageData=courierService.findPageData(specification,pageable);
		pushPageDataToValueStack(pageData);
		 return SUCCESS;
	 }
	
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}
	@Action(value="courier_dele",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String del(){
		String[] id = ids.split(",");
		System.out.println(id);
		courierService.del(id);
		
		return SUCCESS;
	}
	@Action(value="courier_up",results={@Result(name="success",type="redirect",location="./pages/base/courier.html")})
	public String up(){
		String[] id = ids.split(",");
		System.out.println(id);
		courierService.up(id);
		
		return SUCCESS;
	}
	@Action(value="courier_findnoassociation",results={@Result(name="success",type="json")})
	public String findnoassociation(){
		
		List<Courier> couriers=courierService.findNoAssonciation();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
